<template>
    <page-view :crumbs-data='crumbsData'>
        <h-card fixed>
            <h-table
                    fixed
                    ref='tableRef'
                    :columns='columns'
                    :data='loadData'
                    :rowSelection='{ selectedRowKeys, onSelect }'>
                        <template #toolbar>
                            <a-button size='small' type='primary' icon='plus' @click="handleAdd('add')">
                                新增
                            </a-button>
                             <a-button type='ghost-warning' size='small' icon='download' @click="handleExportXls('data')" >
                                导出
                            </a-button>
                            <a-button size='small' type='ghost-success' icon='import' @click="handleImportExcel">导入
                            </a-button>
                            <a-popconfirm v-if='selectedRowKeys.length' title='确定删除吗?' @confirm='handleBatchDelete'>
                                <a-button size='small' type='danger' ghost icon='delete'>
                                    批量删除
                                </a-button>
                            </a-popconfirm>
                        </template>
                        <a-space slot='actions' slot-scope='{ row }'>
                            <a @click="handleEdit(row, 'edit')"> 编辑 </a>
                            <a-popconfirm title='确定删除吗?' @confirm='handleDelete(row.id)'>
                                <a class='danger-text'>删除</a>
                            </a-popconfirm>
                        </a-space>
            </h-table>
             <h-file-import
                    ref='HFileImport'
                    :isHistory='false'
                    @change='refresh'
                    @downloadExcel='handleExportXls("template")'
            />
            <${webPrefixMiddle}-modal  ref='modalRef' @change='refresh' />
        </h-card>
    </page-view>
</template>

<script>
    import PageView from '@/views/components/layouts/PageView.vue';
    import ${webPrefix}Modal from './${webPrefix}Modal.vue';
    import { getSelectOptions } from '@/utils/formUtil';
    import { tableColumns, busiFieldFormSelectConfig, busiFieldSearchSelectConfig } from './data';
    import { get } from "xe-utils";
    import HFileImport from '@/views/components/HFileImport.vue';
    const baseUrlPrefix = process.env.VUE_APP_API_BASE_URL_PREFIX;
    export default {
        name: '${webPrefix}List',
        components: { ${webPrefix}Modal, HFileImport, PageView},
        data() {
            return {
                crumbsData: [
                    {
                        title: "${tableTxt}",
                        icon: 'appstore',
                        currentRoute: true,
                        rollback: false
                    }
                ],
                selectedRows: [],
                selectedRowKeys: [],
                columns: tableColumns,
                loadData: (params) => {
                    let data = {
                        ...params,
                    };
                    this.selectedRows = [];
                    this.selectedRowKeys = [];
                    return this.$POST(this.url.list, data).then((res) => {
                        if (res.code === 200) return res.data;
                    });
                },
                url: {
                    list: baseUrlPrefix + '/${tableName}Controller/list', // 带分页list
                    listAll: baseUrlPrefix + '/${tableName}Controller/listAll', // 不带分页list
                    delete: baseUrlPrefix + '/${tableName}Controller/delete',
                    export: baseUrlPrefix + '/${tableName}Controller/exportExcel',
                    import: baseUrlPrefix + '/${tableName}Controller/importExcel'
                }
            };
        },
        created(){
            getSelectOptions(busiFieldFormSelectConfig, this.columns, 'options.options')
            getSelectOptions(busiFieldSearchSelectConfig, this.columns, 'options.options')
        },
        methods: {
            refresh() {
                this.selectedRowKeys = [];
                this.selectedRows = [];
                this.$refs.tableRef.refresh();
            },
            handleBatchDelete() {
                if (!this.selectedRowKeys.length) return this.$message.warning('请至少选择一条数据');
                this.handleDelete(this.selectedRowKeys.join(','));
            },
            handleDelete(id) {
                this.deleteRequest(id);
            },
            deleteRequest(id) {
                this.$POST(this.url.delete, { id }).then((res) => {
                    const { code = 0 } = res;
                    if (code === 200) {
                        this.$message.success('删除成功');
                        this.refresh();
                    }
                });
            },
            handleEdit(record, type) {
                this.$refs.modalRef.show(record, type);
            },
            handleAdd(type) {
                this.$refs.modalRef.show({}, type);
            },
            onSelect(selectedRowKeys, selectedRows) {
                this.selectedRowKeys = selectedRowKeys;
                this.selectedRows = selectedRows;
            },
            handleImportExcel() {
                this.$refs.HFileImport.show("${tableTxt}", this.url.import);
            },
            handleExportXls(operationType = 'data') {
                let params = {
                    ...(get(this, "$refs.tableRef.queryParams", {})),
                    operationType,
                    ids: this.selectedRowKeys.toString(),
                    fileName: "${tableTxt}.xls",
                };
                let url = this.url.export;
                //params.queryParam = JSON.stringify(params.queryParam);
                //params.sortParam = JSON.stringify(params.sortParam);
                this.$DOWNLOADBLOB(url, params, "POST");
            },
        }
    };
</script>

<style scoped lang='less'>

</style>